package com.villaday.idf.activity3;

import android.graphics.Color;
import android.os.Build;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.pop.SendEmailPop;
import com.villaday.idf.model3.Orders_invoice;
import com.villaday.idf.model3.orders_invoiceSendEmailEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.StatusBarUtils;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * 发送邮箱
 *
 * */
public class FapiaoSendEmailActivity extends BaseActivity {


    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.et_email)
    EditText etEmail;
    private LoadingDialog loadingDialog;
    private Gson gson=new GsonBuilder().create();

    @Override
    protected int getViewResId() {
        return R.layout.activity_fapiao_send_email;
    }

    @Override
    protected void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            StatusBarUtils.setStatusBarLightMode(this, Color.parseColor("#FFF7F9FB"));
        ToolBarUtil.initToolBar(toolbarText, "发送邮箱", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        String email = getIntent().getStringExtra("email");
        etEmail.setText(email);
    }

    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etEmail.getText().toString())) {
            ToastUtil.initToast(this, "请输入电子邮箱");
        } else {
            loadingDialog = LoadingDialog.newInstance("提交中...");
            loadingDialog.show(getFragmentManager());
            final Orders_invoice orders_invoiceEntity = new Orders_invoice(getIntent().getStringExtra("orderNo"),
                    etEmail.getText().toString());
            Map<String, Object> stringObjectsHashMap = new HashMap<>();
            OkHttpUtil.postJson(this, Constant.URL.orders_invoice, orders_invoiceEntity, stringObjectsHashMap, new OkHttpUtil.OnDataListener() {
                @Override
                public void onResponse(String url, String json) {
                    dismissLoading();
                    if (!TextUtils.isEmpty(json)){
                        LogeUtil.e("orders_invoice",json);
                        orders_invoiceSendEmailEntity orders_invoiceSendEmailEntity = gson.fromJson(json, orders_invoiceSendEmailEntity.class);
                       if (Constant.Strings.SUC.equals(orders_invoiceSendEmailEntity.getCode())&&orders_invoiceSendEmailEntity.getBody().isIsSuccess()){
                           SendEmailPop sendEmailPop = new SendEmailPop(FapiaoSendEmailActivity.this, orders_invoiceSendEmailEntity.getBody().getMsg());
                           sendEmailPop.showPopupWindow();
                       }else {
                           ToastUtil.initToast(FapiaoSendEmailActivity.this,(orders_invoiceSendEmailEntity.getBody()==null||TextUtils.isEmpty(orders_invoiceSendEmailEntity.getBody().getMsg()))?orders_invoiceSendEmailEntity.getMsg():orders_invoiceSendEmailEntity.getBody().getMsg());

                       }

                    }

                }

                @Override
                public void onFailure(String url, String error) {
                    dismissLoading();
                    ToastUtil.initToast(FapiaoSendEmailActivity.this,error);

                }
            });
        }
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }
}
